package com.example.elmmerchants.First_Page_Fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

import com.example.elmmerchants.Auto_Get_Order_Page;
import com.example.elmmerchants.Order_Page.Order_Data;
import com.example.elmmerchants.Order_Setting.Order_Setting_Page;
import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Order_Page extends Fragment {

    View root, all_order_view, all_after_sales_view;
    Spinner choose_which_day;
    TextView after_sales_text_view, all_order_text_view, all_button, cancel_text_view, user_refund, meal_damage_compensation, is_open_auto_order_text_view;
    RelativeLayout set_which_order, after_sales_set_check_which_give_money;
    ScrollView all_order_list_view, after_sales_list_view, cancel_order_list_view, food_bad_refund_list_view;
    LinearLayout which_get_order_method, order_layout;
    ImageButton order_setting;

    Map<String, String> month = new HashMap<>();
    List<String> this_7_day = new ArrayList<>();
    String email;
    boolean is_open_auto_get_order = false;
    Map<String, Order_Data> order_data = new HashMap<>();
    boolean is_loading_data = false, is_loading_order_page_data = false, is_new_order = false;

    public Order_Page(String email) {
        this.email = email;
    }

    private class Shop_Data_Thread extends Thread {
        @Override
        public void run() {
            is_loading_order_page_data = false;
            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    String sql = "SELECT * FROM merchant WHERE email = ?;";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, email);
                    ResultSet set = preparedStatement.executeQuery();
                    while (set.next()) {
                        if (set.getInt("is_open_auto") == 1) {
                            is_open_auto_get_order = true;
                        }
                        else {
                            is_open_auto_get_order = false;
                        }
                        is_loading_order_page_data = true;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_loading_order_page_data) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (isVisible()) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (is_open_auto_get_order) {
                            is_open_auto_order_text_view.setText("自动接单中");
                        }
                        else {
                            is_open_auto_order_text_view.setText("手动接单中");
                        }
                    }
                });
            }
            else {
                onStop();
            }
        }
    }

    private class Order_Data_Thread extends Thread {
        @Override
        public void run() {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    View view = LayoutInflater.from(root.getContext()).inflate(R.layout.order_card_view, null);
                    CardView order_card_view = view.findViewById(R.id.order_card_view);
                    order_card_view.setBackgroundResource(R.drawable.border_white);

                    order_layout.addView(view);
                }
            });
        }
    }

    private class Check_Is_New_Order_Thread extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(5);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (is_new_order) {

                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_order__page, container, false);

        choose_which_day = root.findViewById(R.id.choose_which_day);
        after_sales_text_view = root.findViewById(R.id.after_sales_text_view);
        all_order_text_view = root.findViewById(R.id.all_order_text_view);
        all_order_view = root.findViewById(R.id.all_order_view);
        set_which_order = root.findViewById(R.id.set_which_order);
        all_order_list_view = root.findViewById(R.id.all_order_list_view);
        after_sales_list_view = root.findViewById(R.id.after_sales_list_view);
        after_sales_set_check_which_give_money = root.findViewById(R.id.after_sales_set_check_which_give_money);
        all_after_sales_view = root.findViewById(R.id.all_after_sales_view);
        all_button = root.findViewById(R.id.all_button);
        cancel_text_view = root.findViewById(R.id.cancel_text_view);
        cancel_order_list_view = root.findViewById(R.id.cancel_order_list_view);
        user_refund = root.findViewById(R.id.user_refund);
        meal_damage_compensation = root.findViewById(R.id.meal_damage_compensation);
        food_bad_refund_list_view = root.findViewById(R.id.food_bad_refund_list_view);
        which_get_order_method = root.findViewById(R.id.which_get_order_method);
        is_open_auto_order_text_view = root.findViewById(R.id.is_open_auto_order_text_view);
        order_setting = root.findViewById(R.id.order_setting);
        order_layout = root.findViewById(R.id.order_layout);

        month.put("Dec", "12");
        month.put("Nov", "11");
        month.put("Oct", "10");
        month.put("Sep", "9");
        month.put("Aug", "8");
        month.put("Jul", "7");
        month.put("Jun", "6");
        month.put("May", "5");
        month.put("Apr", "4");
        month.put("Mar", "3");
        month.put("Feb", "2");
        month.put("Jan", "1");

        for (int i = 0; i < 7; i++) {
            String this_7_day_string = "";
            Calendar cal = GregorianCalendar.getInstance();
            cal.setTime(new Date());
            cal.add(Calendar.DAY_OF_YEAR, -i);
            Date daysBeforeDate = cal.getTime();
            String day = daysBeforeDate.toString();
            String month_string = "";
            month_string += day.charAt(4);
            month_string += day.charAt(5);
            month_string += day.charAt(6);
            this_7_day_string += month.get(month_string) + "月";
            this_7_day_string += day.charAt(8);
            this_7_day_string += day.charAt(9) + "日";
            this_7_day.add(this_7_day_string);
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<>(root.getContext(),
                android.R.layout.simple_list_item_activated_1, this_7_day);
        choose_which_day.setAdapter(adapter);

        Shop_Data_Thread shopDataThread = new Shop_Data_Thread();
        shopDataThread.start();

        Order_Data_Thread orderDataThread = new Order_Data_Thread();
        orderDataThread.start();

        all_order_text_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                all_order_text_view.setTextColor(Color.BLACK);
                after_sales_text_view.setTextColor(Color.rgb(128, 128, 128));
                all_order_view.setVisibility(View.VISIBLE);
                set_which_order.setVisibility(View.VISIBLE);
                all_order_list_view.setVisibility(View.VISIBLE);
                all_after_sales_view.setVisibility(View.GONE);
                after_sales_set_check_which_give_money.setVisibility(View.GONE);
                after_sales_list_view.setVisibility(View.GONE);
                food_bad_refund_list_view.setVisibility(View.GONE);
                all_button.setTextColor(Color.BLACK);
                cancel_text_view.setTextColor(Color.rgb(128, 128, 128));
            }
        });

        after_sales_text_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                after_sales_text_view.setTextColor(Color.BLACK);
                all_order_text_view.setTextColor(Color.rgb(128, 128, 128));
                all_after_sales_view.setVisibility(View.VISIBLE);
                after_sales_set_check_which_give_money.setVisibility(View.VISIBLE);
                after_sales_list_view.setVisibility(View.VISIBLE);
                all_order_view.setVisibility(View.GONE);
                set_which_order.setVisibility(View.GONE);
                all_order_list_view.setVisibility(View.GONE);
                cancel_order_list_view.setVisibility(View.GONE);
                user_refund.setTextColor(Color.BLACK);
                meal_damage_compensation.setTextColor(Color.rgb(128, 128, 128));
            }
        });

        all_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                all_button.setTextColor(Color.BLACK);
                cancel_text_view.setTextColor(Color.rgb(128, 128, 128));
                all_order_list_view.setVisibility(View.VISIBLE);
                cancel_order_list_view.setVisibility(View.GONE);
            }
        });

        cancel_text_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cancel_text_view.setTextColor(Color.BLACK);
                all_button.setTextColor(Color.rgb(128, 128, 128));
                cancel_order_list_view.setVisibility(View.VISIBLE);
                all_order_list_view.setVisibility(View.GONE);
            }
        });

        user_refund.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                user_refund.setTextColor(Color.BLACK);
                meal_damage_compensation.setTextColor(Color.rgb(128, 128, 128));
                after_sales_list_view.setVisibility(View.VISIBLE);
                food_bad_refund_list_view.setVisibility(View.GONE);
            }
        });

        meal_damage_compensation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                meal_damage_compensation.setTextColor(Color.BLACK);
                user_refund.setTextColor(Color.rgb(128, 128, 128));
                food_bad_refund_list_view.setVisibility(View.VISIBLE);
                after_sales_list_view.setVisibility(View.GONE);
            }
        });

        which_get_order_method.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(root.getContext(), Auto_Get_Order_Page.class);
                intent.putExtra("email", email);
                intent.putExtra("is_open_auto_get_order", is_open_auto_get_order);
                startActivityForResult(intent, 1);
            }
        });

        order_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(root.getContext(), Order_Setting_Page.class);
                intent.putExtra("email", email);
                intent.putExtra("is_open_auto_get_order", is_open_auto_get_order);
                startActivityForResult(intent, 1);
            }
        });

        return root;
    }

    public void order() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1: {
                Shop_Data_Thread shopDataThread = new Shop_Data_Thread();
                shopDataThread.start();
                break;
            }
        }
    }
}